package com.you.concurrent.juc.semaphore;

import java.util.concurrent.Semaphore;

public class SemaphoreDemo {

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(5); //5个许可
        for (int i = 0; i < 20; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        semaphore.acquire(); //获取许可
                        Thread.sleep(1000L);
                        System.out.println(System.currentTimeMillis() + " " + Thread.currentThread().getName() +
                                "----finish---");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        semaphore.release(); //释放许可
                    }
                }
            }).start();
        }
    }
}
